package myJs.tool

import myJs.myPkg.jquery.$
import org.scalajs.dom.window

import scala.scalajs.js.{Date, |}
import myJs.Implicits._
import myJs.api.Api
import myJs.myPkg.LayerOptions
import org.scalajs.dom.window.localStorage
import scalatags.Text.all._
import slinky.web.html.s
import typings.csstype.csstypeStrings

import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.typedarray.Uint8Array

/**
 * Created by yz on 24/4/2022
 */
object Tool extends ProjectTool {

  val pattern = "yyyy-mm-dd"

  val zhInfo = "信息"
  val layerOptions = LayerOptions(title = zhInfo, closeBtn = 0, skin = "layui-layer-molv", btn = List())

  val phoneRegexp = js.RegExp("^((00|\\+)?(86(?:-| )))?((\\d{11})|(\\d{3}[- ]{1}\\d{4}[- ]{1}\\d{4})|((\\d{2,4}[- ]){1}(\\d{7,8}|(\\d{3,4}[- ]{1}\\d{4}))([- ]{1}\\d{1,4})?))$")
  val integerRegexp = js.RegExp("^(?:-?(?:0|[1-9][0-9]*))$")

  val zhRunning = "正在运行"
  val myElement = div(id := "content")(
    span(id := "info")(zhRunning),
    " ",
    img(src := s"${Api.images}/running2.gif", width := 30, height := 20, cls := "runningImage")
  ).render

  val confirmButtonClass = "btn-danger"
  val cancelButtonClass = "green"

  def betweenValidate(min: Int, max: Int, info: String) = (v: String) => {
    val valid = v.isDouble && v.toDouble >= min && v.toDouble <= max
    val rs = if (valid) {
      valid
    } else s"${info}必须处于${min}到${max}之间！"
    rs.asInstanceOf[Boolean | String]
  }

  def timeBefore = (dateStr: String, dbDateStr: String) => {
    val date = new Date(dateStr)
    val dbDate = new Date(dbDateStr)
    dbDate.setUTCHours(0, 0, 0, 0)
    date.getTime() <= dbDate.getTime()
  }

  def timeAfter = (dateStr: String, dbDateStr: String) => {
    val date = new Date(dateStr)
    val dbDate = new Date(dbDateStr)
    dbDate.setUTCHours(0, 0, 0, 0)
    date.getTime() >= dbDate.getTime()
  }

  def b2Display(b: Boolean) = {
    if (b) csstypeStrings.block else csstypeStrings.none
  }

  def selectLeft: Unit = {
    val idInfo = window.location.href.replaceAll(".*/#/", "").
      replaceAll("\\?.*$", "")
    selectLeft(idInfo)
  }

  def selectLeft(idInfo: String): Unit = {
    val tmpIdInfos = idInfo.split("/").toList
    val idInfos = tmpIdInfos.size match {
      case 2 | 3 => tmpIdInfos.drop(1)
      case _ => tmpIdInfos
    }
    val mainId = idInfos.head
    val liId = idInfos.getOrElse(1, "")
    val menuContainer = $(".page-sidebar ul")
    menuContainer.children("li.active").removeClass("active")
    menuContainer.children("li").children("ul").hide()
    $(s"#${mainId} ul>li").removeClass("open")
    $(s"#${mainId}").addClass("active")
    if (liId.nonEmpty) {
      $(s"#${mainId} ul").show()
      $(s"#${mainId} #${liId}").addClass("open")
    }
  }

  def getAccount = {
    localStorage.getItem("account")
  }

  def getToken = {
    localStorage.getItem("token")
  }

  def getInputByName(name: String) = {
    $(s":input[name='${name}']")
  }

  def formatDate(time: Date) = {
    val year = time.getFullYear().toInt
    val month = time.getMonth().toInt + 1
    val day = time.getDate().toInt
    s"${year}-${zeroFill(month)}-${zeroFill(day)}"
  }

  def zeroFill(int: Int) = {
    if (int < 10) s"0${int}" else int.toString
  }

  def formatDateTime(time: Date, showYear: Boolean = true, showSecond: Boolean = true) = {
    val year = time.getFullYear().toInt
    val month = time.getMonth().toInt + 1
    val day = time.getDate().toInt
    val hour = time.getHours().toInt
    val minute = time.getMinutes().toInt

    val secondStr = if (showSecond) {
      val second = time.getSeconds().toInt
      s":${zeroFill(second)}"
    } else ""
    if (showYear) {
      s"${year}-${zeroFill(month)}-${zeroFill(day)} ${zeroFill(hour)}:${zeroFill(minute)}${secondStr}"
    } else {
      s"${zeroFill(month)}-${zeroFill(day)} ${zeroFill(hour)}:${zeroFill(minute)}${secondStr}"
    }
  }

  def base642binaryArray(content: String) = {
    val byteCharacters = window.atob(content)
    val sliceSize = 512
    val byteArrays = byteCharacters.grouped(sliceSize).map { slice =>
      val byteNumbers = slice.map(_.toShort).toJSArray
      new Uint8Array(byteNumbers)
    }
    byteArrays.toJSArray.map(_.asInstanceOf[js.Any])
  }

}
